package com.cennavi.funcs;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.spark.api.java.function.Function2;

import com.cennavi.util.DateUtil;

import scala.Tuple2;

/** * @author  作者 :allen
    * @date 创建时间：2017年11月15日 下午7:47:47 
    * @version 1.0 
    */

public class GetMinTimeFunction 
	implements Function2<Tuple2<String, String>, Tuple2<String, String>, Tuple2<String, String>> {

	private static final long serialVersionUID = 1L;

	public Tuple2<String, String> call(Tuple2<String, String> arg0, Tuple2<String, String> arg1) throws Exception {
		// 把window中的所有数据逐条对比，保留每次对比中较小的时间
		String time1;
		String time2;
		if(arg0._2.split(",").length < 4){
			time1 = arg0._2;
		}else{
			time1 = arg0._2.split(",")[3];			
		}
		if(arg1._2.split(",").length < 4){
			time2 = arg1._2;
		}else{
			time2 = arg1._2.split(",")[3];
		}
		Tuple2<String, String> timeAfterCompare;
		Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time1);
		Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time2);
		if(DateUtil.compTime(date1, date2)){
			timeAfterCompare = new Tuple2<String, String>("minTime", time1);
		}else{
			timeAfterCompare = new Tuple2<String, String>("minTime", time2);
		}
		return timeAfterCompare;
	}

}
